package com.blindnews.utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NewsContent1 {
//    网易新闻
    private static int count=0;

    public static void main(String[] args) throws IOException {



        // 设置请求地址
        List<String> strings=new ArrayList<>();
        strings.add("https://www.163.com/dy/article/IT104JLI051482MP.html");
        strings.add("https://www.163.com/dy/article/IT0T6901051482MP.html");
        String url = "https://www.163.com/dy/article/IU5KBS01055040N3.html";


        // 设置请求头
        String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36";


            try{
                // 获取详情页数据
                Document detailDoc = Jsoup.connect(url).header("User-Agent", userAgent).get();
                System.out.println(detailDoc.text());
                // 获取新闻详情,select获取Post_body的所有子标签；
                Elements newsTitle = detailDoc.select("div.post_main>h1");//获得标题
                System.out.println("Title："+newsTitle.text());
                Elements newsContent = detailDoc.select("div.post_body>*");//获得正文

                String[] newsListArr = newsContent.text().split(" ");
                System.out.println("Conten："+newsListArr.length+"行");
                StringBuilder stringBuilder = new StringBuilder();

                String content=null;

                for (String news : newsListArr) {
                    count+=1;
//                将空内容标签过滤掉
                    if(news.isEmpty()){
                        continue;
                    }
//                遇到延伸阅读，不再获取，跳出循环
                    if(news.equals("延伸阅读"))
                    {
                        break;
                    }
                    System.out.println("第"+count+"段");
                    System.out.println(news);
                    stringBuilder.append(news);
                }
                content=stringBuilder.toString();
                System.out.println(content);
            } catch (IOException e) {
                e.printStackTrace();
            }




    }
}

